package com.jason.project.CloseScreen;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;

public class DeviceAdmin extends DeviceAdminReceiver {
	public static class CloseScreenActivity extends Activity {
		static final int REQUEST_DEVICE_ADMIN = 1;

		DevicePolicyManager mDPM = null;
		ComponentName mDeviceAdminSample = null;

		public void closeScreen() {
			try {
				mDPM.lockNow();

				Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
				long[] pattern = { 0, 50, 200 };
				vibrator.vibrate(pattern, -1);

			} catch (Exception e) {
				new AlertDialog.Builder(this)
						.setCancelable(true)
						.setTitle(R.string.error)
						.setMessage(R.string.error_failed_close_screen)
						.setPositiveButton(R.string.ok,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int whichButton) {
									}
								}).create().show();
				Log.d(getString(R.string.error), e.toString());
			} finally {
			}
		}

		private void init() {
			mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
			mDeviceAdminSample = new ComponentName(this, DeviceAdmin.class);
		}

		private boolean isAdminActive() {
			return mDPM.isAdminActive(mDeviceAdminSample);
		}

		@Override
		protected void onActivityResult(int requestCode, int resultCode,
				Intent data) {
			switch (requestCode) {
			case REQUEST_DEVICE_ADMIN:
				if (resultCode == Activity.RESULT_OK) {
					closeScreen();
					finish();

				} else {
					new AlertDialog.Builder(this)
							.setCancelable(false)
							.setTitle(R.string.prompt_request_device_admin)
							.setMessage(
									R.string.desc_retry_request_device_admin)
							.setPositiveButton(R.string.yes,
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
											requestAdminActive();
										}
									})
							.setNegativeButton(R.string.no,
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
											finish();
										}
									}).create().show();
				}
				return;
			}

			super.onActivityResult(requestCode, resultCode, data);
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

			init();

			if (!isAdminActive()) {
				requestAdminActive();
			} else {
				closeScreen();
				finish();
			}
		}

		private void requestAdminActive() {
			Intent intent = new Intent(
					DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
			intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
					mDeviceAdminSample);
			intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
					R.string.desc_request_device_admin);
			startActivityForResult(intent, REQUEST_DEVICE_ADMIN);
		}
	}

}